package com.newmainsoftech.dao;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;

import javax.ejb.embeddable.EJBContainer;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import com.newmainsoftech.eatestutil.container.glassfish.EmbeddedGlassfishConfig;
import com.newmainsoftech.injection.AbstractAnnoSwapperAspect;

/**
 * Aspect to swap <code>EjbContainerMate</code> instance in test classes.
 * 
 * @author Arata Y.
 */
@Aspect
public class EjbContainerSwapperAspect extends AbstractAnnoSwapperAspect {
	private static GlassfishEjbContainerMate glassfishEjbContainerMate = new GlassfishEjbContainerMate();
		static {
			glassfishEjbContainerMate.getEmbeddedGlassfishConfig().setKeepTemporaryFiles( true);
			glassfishEjbContainerMate.configureGlassfishDaoLoggers( Level.FINEST);
			
			final EmbeddedGlassfishConfig containerConfig
			= glassfishEjbContainerMate.getEmbeddedGlassfishConfig();
			
			Map<String, Object> ejbContainerPropertyMap = new HashMap<String, Object>();
				ejbContainerPropertyMap.put( 
						EJBContainer.MODULES, glassfishEjbContainerMate.getEjbModuleStageDir());
				final Entry<String, Object> configurationFilePropertyEntry 
				= containerConfig.getEmbeddedGlassfishConfigurationFilePropertyEntry( 
						glassfishEjbContainerMate.getDomainXmlFile().getPath());
				ejbContainerPropertyMap.put(
						configurationFilePropertyEntry.getKey(), configurationFilePropertyEntry.getValue());
							/* Setting only org.glassfish.ejb.embedded.glassfish.configuration.file property to 
							 * point at custom domain.xml, in order to make embedded glassfish automatically 
							 * yield temporary domain directory with config, docroot, lib, and META-INF 
							 * sub-directories. 
							 * custom domain.xml being pointed by org.glassfish.ejb.embedded.glassfish.configuration.file 
							 * property must have authorization service configuration: 
							 * https://java.net/projects/glassfish/lists/dev/archive/2013-03/message/112 
							 */
				
				Entry<String, Object> webHttpPortPropertyEntry
				= containerConfig.getEmbeddedGlassfishWebHttpPortPropertyEntry( 
						String.valueOf( EjbTestCommonProperty.getJeeContainerWebHttpPort()));
				ejbContainerPropertyMap.put( 
						webHttpPortPropertyEntry.getKey(), webHttpPortPropertyEntry.getValue());
					
			final EJBContainer eJBContainer = EJBContainer.createEJBContainer( ejbContainerPropertyMap);
			
			glassfishEjbContainerMate.setEjbContainer( eJBContainer);
			glassfishEjbContainerMate.setContext( eJBContainer.getContext());
		}
		public static GlassfishEjbContainerMate getGlassfishEjbContainerMate() {
			return glassfishEjbContainerMate;
		}
		public static void setGlassfishDaoLogLevel( final Level level) {
			getGlassfishEjbContainerMate().configureGlassfishDaoLoggers( level);
		}

	@Override
	public Object getReplacement() {
		return EjbContainerSwapperAspect.getGlassfishEjbContainerMate();
	}

	@Override
	@Pointcut( value = "within( com.newmainsoftech.dao..*)") 
	public void pointcutToScreenSnapperScope() {}
}
